﻿using System;
using System.Collections.Generic;
using System.Drawing;
//using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace KeyEnter
{
    class Win32
    {
        public const int SRCCOPY = 0x00CC0020;
        public const int WM_KEYDOWN = 0x0100;
        public const int WM_KEYUP = 0x0101;

        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄       
        public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

        [DllImport("user32.dll")]
        public static extern int GetCursorPos(ref Point lpPoint);  //获取鼠标坐标，该坐标是光标所在的屏幕坐标位置

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]

        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", EntryPoint = "keybd_event")]
        public static extern void keybd_event(
       byte bVk, //虚拟键值  
       byte bScan,// 一般为0  
       int dwFlags, //这里是整数类型 0 为按下，2为释放  
       int dwExtraInfo //这里是整数类型 一般情况下设成为0  
       );


        //消息发送API
        [DllImport("User32.dll", EntryPoint = "PostMessage")]
        public static extern int PostMessage(
            IntPtr hWnd,        // 信息发往的窗口的句柄
           int Msg,            // 消息ID
            int wParam,         // 参数1
            int lParam            // 参数2
        );

        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern IntPtr GetDC(IntPtr Hwnd); //其在MSDN中原型为HDC GetDC

        public struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, out Rect rect);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);// handle to DC

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(
          IntPtr hdc,        // handle to DC
          int nWidth,     // width of bitmap, in pixels
          int nHeight     // height of bitmap, in pixels
          );

        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(
           IntPtr hdc,          // handle to DC
           IntPtr hgdiobj   // handle to object
           );

        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
        int nWidth, int nHeight, IntPtr hObjectSource,
        int nXSrc, int nYSrc, int dwRop);

        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hDC);

        [DllImport("user32.dll")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);

    }
}
